package net.jgrm.common.exception;

/**
 * 通用业务异常
 *
 * @author service-common
 * @date 2024-09-12
 */
public class CommonException extends RuntimeException {

    /**
     * 异常编码
     */
    private Integer code;

    /**
     * 异常信息
     */
    private String message;

    /**
     * 基于异常枚举构造
     *
     * @param exceptionEnum 异常枚举
     */
    public CommonException(BaseExceptionEnum exceptionEnum) {
        super(exceptionEnum.getMessage());
        this.code = exceptionEnum.getCode();
        this.message = exceptionEnum.getMessage();
    }

    /**
     * 基于编码和消息构造
     *
     * @param code    异常编码
     * @param message 异常信息
     */
    public CommonException(Integer code, String message) {
        super(message);
        this.code = code;
        this.message = message;
    }

    /**
     * 基于消息构造，使用默认编码500
     *
     * @param message 异常信息
     */
    public CommonException(String message) {
        super(message);
        this.code = 500;
        this.message = message;
    }

    public Integer getCode() {
        return this.code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    @Override
    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public String toString() {
        return "CommonException{" +
                "code=" + code +
                ", message='" + message + '\'' +
                '}';
    }
}